/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.validators;

import cz.muni.fi.pa036is.dto.RoundFormTO;
import cz.muni.fi.pa036is.entity.Round;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author Kuba
 */
public class RoundValidator implements Validator{

    @Override
    public boolean supports(Class<?> type) {
        return RoundFormTO.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
        
        ValidationUtils.rejectIfEmpty(errors, "pointCount1", "pointCount1.empty", "Po");
        ValidationUtils.rejectIfEmpty(errors, "pointCount2", "pointCount2.empty");
        ValidationUtils.rejectIfEmpty(errors, "sequenceNumber", "sequenceNumber.empty");
    
    
        RoundFormTO round = (RoundFormTO) o;

        if (round.getPointCount1() < 0) {
            errors.reject("pointCount1", "Attribute Point Count for player 1 has to be greater or equal to 0.");
        }

        if (round.getPointCount2() < 0) {
            errors.reject("pointCount2", "Attribute Point Count for player 2 has to be greater or equal to 0.");
        }
        
        if (round.getSequenceNumber() < 1) {
            errors.reject("sequenceNumber", "Attribute Sequence Number has to be greater than 0.");
        }
    
}
    }
